import {HYEventStore} from 'hy-event-store'
import { getCategory,getDetailsData,getServiceList } from '../services/modules/home_services'

const homeStore = new HYEventStore({
  state: {
    classifyLists: [],
    serviceList: [],
    details: {}
  },
  actions : {
    // 获取分类
    fetchCategoryAction(ctx) {
      getCategory().then(res => {
        ctx.classifyLists = res.data
      })
    },
    // 获取列表
    fetchServiceListAction(ctx, page, type, category_id, hasEmpty) {
      getServiceList(page, type, category_id).then(res => {

        // 是否清空数据
        if (hasEmpty === 'empty') {
          ctx.serviceList = []
        }

        if (res.data.data.length > 0) {
          let data = ctx.serviceList.concat(res.data.data)
          ctx.serviceList = data
        } else {
          wx.showToast({
            title: '没有更多数据了',
            icon: 'none',
            duration: 1000
          })
        }
      })
    },

    // 获取详情
    fetchDetailsAction(ctx, id) {
      getDetailsData(id).then(res => {
        ctx.details = res.data
      })
    }
  }
})

export default homeStore